// FavoritesServiceImpl.java 实现类
package com.hadluo.qq.server.service.impl;

import com.hadluo.qq.server.entity.Favorites;
import com.hadluo.qq.server.entity.User;
import com.hadluo.qq.server.mapper.FavoritesMapper;
import com.hadluo.qq.server.mapper.UserMapper;
import com.hadluo.qq.server.service.FavoritesService;
import com.hadluo.qq.server.vo.FavoriteVO;
import com.hadluo.qq.server.vo.FriendVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class FavoritesServiceImpl implements FavoritesService {

    @Autowired
    private FavoritesMapper favoritesRepository;

    @Autowired
    private UserMapper userRepository;

    @Override
    @Transactional
    public boolean addFavorite(Long userId, Long friendId) {
        // 检查是否已存在
        if (isFavorite(userId, friendId)) {
            return false;
        }

        // 检查好友是否存在
        User friendOptional = userRepository.selectById(friendId);
        if (friendOptional==null) return false;

        Favorites favorite = new Favorites();
        favorite.setUserId(userId);
        favorite.setFriendId(friendId);
        favorite.setCreateTime(new Date());
        favorite.setUpdateTime(new Date());
        favoritesRepository.insert(favorite);
        return true;
    }

    @Override
    @Transactional
    public boolean removeFavorite(Long userId, Long friendId) {
        return favoritesRepository.deleteByUserIdAndFriendId(userId, friendId) > 0;
    }

    @Override
    public List<FriendVO> getFavoriteList(Long userId) {
        List<Favorites> favorites = favoritesRepository.selectByUserId(userId);
        List<FriendVO> friendVOs = new ArrayList<>();

        for (Favorites favorite : favorites) {
            FriendVO friendVO = new FriendVO();
       // 设置好友信息
            User friend = userRepository.selectById(favorite.getFriendId());
            if (friend!=null) {
                friendVO.setQqNumber(friend.getQqNumber());
                friendVO.setNickname(friend.getNickname());
                friendVO.setAvatar(friend.getAvatar());
                friendVO.setSignature(friend.getSignature());
                friendVO.setId(favorite.getFriendId());
                friendVO.setStatus(friend.getStatus());
            }

            System.out.println(friendVO);
            friendVOs.add(friendVO);
        }

        return friendVOs;
    }

    @Override
    public FavoriteVO getFavoriteDetail(Long userId, Long friendId) {
        Favorites favorites = favoritesRepository.selectByUserIdAndFriendId(userId, friendId);
        if (favorites==null) {
            return null;
        }
        
        FavoriteVO favoriteVO = new FavoriteVO();
        BeanUtils.copyProperties(favorites, favoriteVO);
        
//        // 设置好友信息
//        User friendOptional = userRepository.selectById(friendId);
//        if (friendOptional==null) {
//            User friend = friendOptional
//            favoriteVO.setFriendQqNumber(friend.getQqNumber());
//            favoriteVO.setFriendNickname(friend.getNickname());
//            favoriteVO.setFriendAvatar(friend.getAvatar());
//            favoriteVO.setFriendSignature(friend.getSignature());
//        }
        
        return favoriteVO;
    }

    @Override
    public boolean isFavorite(Long userId, Long friendId) {
        Favorites favorites = favoritesRepository.selectByUserIdAndFriendId(userId, friendId);
        return favorites!=null;
    }

    @Override
    @Transactional
    public boolean deleteFavorite(Long userId, Long friendId) {
        // 参数校验
        if (userId == null || userId <= 0 || friendId == null || friendId <= 0) {
            return false;
        }
        try {
            // 执行删除操作
            int rowsAffected = favoritesRepository.deleteByUserIdAndFriendId(userId, friendId);
            // 判断是否删除成功
            boolean success = rowsAffected > 0;
            return success;
        } catch (DataAccessException e) {
            // 数据库操作异常处理
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }
}